package com.smsc.headend.task.engine.registry;

import com.smsc.headend.module.protocol.ProtocolType;
import com.smsc.headend.task.engine.service.protocol.PacketDecodeService;
import com.smsc.headend.task.engine.service.protocol.imp.AnsiPacketDecodeServiceImpl;
import com.smsc.headend.task.engine.service.protocol.imp.DLMSPacketDecodeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class PacketDecodeRegistry {
    @Autowired
    AutowireCapableBeanFactory autowireCapableBeanFactory;

    Map<Long, PacketDecodeService> protocolDataProcessList = new HashMap<>();

    DLMSPacketDecodeServiceImpl dlmsPacketDecodeService;

    AnsiPacketDecodeServiceImpl ansiPacketDecodeService;

    @PostConstruct
    public void load() {
        dlmsPacketDecodeService = new DLMSPacketDecodeServiceImpl();
        ansiPacketDecodeService = new AnsiPacketDecodeServiceImpl();
        autowireCapableBeanFactory.autowireBean(dlmsPacketDecodeService);
        protocolDataProcessList.put(ProtocolType.DLMS_COSEM_46_Protocol.getProtocolId(), dlmsPacketDecodeService);
        protocolDataProcessList.put(ProtocolType.DLMS_COSEM_47_Protocol.getProtocolId(), dlmsPacketDecodeService);
        protocolDataProcessList.put(ProtocolType.DLMS_Gateway_Protocol.getProtocolId(), dlmsPacketDecodeService);
        protocolDataProcessList.put(ProtocolType.ANSI_C12_18_Protocol.getProtocolId(), ansiPacketDecodeService);
    }

    public PacketDecodeService getService(Long protocolId) {
        PacketDecodeService protocolDataProcess =  protocolDataProcessList.get(protocolId);
        if (protocolDataProcess == null) {
            log.error("{} have no Protocol PacketDecodeService", protocolId);
        }
        return protocolDataProcess;
    }
}
